#include "acengine.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static void * my_alloc(void *dummy, size_t size)
{
	return malloc(size);
}

static void my_free(void *dummy, void *pointer)
{
	free(pointer);
}

static AceAction my_callback(void *context, size_t offset, void *key)
{
	int *keyword_count = (int *)context;
	
	keyword_count[0] += keyword_count[(int)key];
	keyword_count[(int)key] = 0;
	
	return ACE_CONTINUE;
}

int main(void)
{
	AceContext context;

	int nr_cases;
	int nr_keywords;
	int index;
	size_t size;
	void *key;

	static char buffer[1000001];
	static int keyword_count[10002];
	
	scanf("%d", &nr_cases);
	
	while (nr_cases--)
	{
		ace_initialize(&context, &my_alloc, &my_free, NULL);
		scanf("%d", &nr_keywords);
		
		keyword_count[0] = 0;
		
		for (index = 1; index <= nr_keywords; ++index)
		{
			scanf("%s", buffer);
			size = strlen(buffer);
			key = (void *)index;
			
			keyword_count[(int)key] = 0;

			if (ace_insert_pattern(&context, (AceByte *)buffer, size, key) == ACE_ALREADY_EXIST)
				ace_retrieve_key(&context, (AceByte *)buffer, size, &key);

			++keyword_count[(int)key];			
		}

		ace_build_automation(&context);
		scanf("%s", buffer);
		ace_append_text(&context, (AceByte *)buffer, strlen(buffer), &my_callback, keyword_count);

		ace_finalize(&context);
		
		printf("%d\n", keyword_count[0]);
	}

	return 0;
}
